<template>
	<view>
		<view class="set-head">
			<p>{{$t('connectNet.change_default_password')}}</p>
			<p>{{$t('connectNet.serial_number')}}：{{device.deviceNo}}</p>
		</view>
		<view class="set-input" style="text-align: center;">
			<uni-easyinput type="password" :suffixIcon="inputicon" v-model="newPass" :placeholder="$t('connectNet.password_length_format')">
			</uni-easyinput>
			<uni-easyinput type="password" :suffixIcon="inputicon" v-model="confirmPass"
				:placeholder="$t('connectNet.password_length_format')"></uni-easyinput>
		</view>
		<view class="u-f-c" style="margin: 20rpx;">
			<view class="u-f-ac" @click="showDesc">
				<img class="info-img" src="https://statics.xmcsrv.net/weixin/Telkomsel/jInfo.png" alt="">
				<view class="reg">{{$t('General.login.pwdRule')}}</view>
			</view>
			<view v-if="isShowDesc" class="pwdDesc" :class="{'red':!isMatch}"> {{$t('General.login.ruleTips')}} </view>
		</view>
		<view class="set-foot">
			*{{$t('connectNet.if_password_not_set')}}
		</view>
		<view class="foot-button">
			<button class="block radius-btn" @tap="nextstep()" type="operation">{{$t('General.login.nextStep')}}</button>
		</view>
	</view>
</template>

<script>
	import {
		XMMD5Encrypt
	} from "@/utils/md5Encrypt.js"
	import {RetCodeMsg} from '@/utils/errorCode.js' 
	// const isPwd = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,64}$/;
	const isPwd = /(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_\-[\]{}?/.<>,';:-]).{8,64}$/;
	const constant = getApp().globalData.constant;
	export default {
		data() {
			return {
				inputicon: 'eye',
				newPass: '',
				confirmPass: '',
				device: {},
				pid: '',
				isShowDesc:false,
				isMatch:true,
			}
		},
		onLoad(opt) {
			uni.setNavigationBarTitle({
				title: this.$t('connectNet.Password')
			});
			// wx.hideHomeButton()
			let enData = decodeURIComponent(opt.device)
			this.device = JSON.parse(enData)
			// this.device = JSON.parse(opt.device)
			console.log(this.device, '设置密码设备信息')
			this.pid = this.device.pid || this.device.deviceType
			console.log(this.pid, '设置密码设备信息pid')
		},
		methods: {
			showDesc(){
				this.isShowDesc = !this.isShowDesc
			},
			nextstep() {
				if (!this.newPass) {
					return uni.showToast({
						title: this.$t('connectNet.enter_password'),
						icon: 'none'
					})
				}
				if (!this.confirmPass) {
					return uni.showToast({
						title: this.$t('connectNet.enter_password'),
						icon: 'none'
					})
				}
				if (this.newPass != this.confirmPass) {
					return uni.showToast({
						title: this.$t('connectNet.passwords_do_not_match'),
						icon: 'none'
					})
				}
				if (!isPwd.test(this.confirmPass)) {
					return uni.showToast({
						title: this.$t('connectNet.enter_correct_password_format'),
						icon: 'none'
					})
				}
				this.setPassWord()
			},

			// 获取设备token
			setPassWord() {
				uni.showLoading({
					title: this.$t('connectNet.loading')
				})
				let sns = []
				sns[0] = this.device.deviceNo
				const data = {
					sns: sns
				};
				getApp().globalData.api.getDeviceToken(data, (result) => {
					uni.hideLoading()
					console.log('获取设备token:', result);
					// this.deviceLogin(result[0].token)
					this.token = result[0].token
					//判断是否是ipc设备
					if (this.pid === parseInt(constant.DEVICE_TYPE.EE_DEV_LOW_POWER, 16) || this.pid.length ===
						16) {
						this.getDeviceStatus(result[0].token, 'viot')
					} else {
						this.getDeviceStatus(result[0].token, 'ipc')
					}
				});
			},
			// 获取设备状态
			getDeviceStatus(token, type) {
				uni.showLoading({
					title: this.$t('connectNet.loading')
				})
				let tokenList = []
				tokenList[0] = token
				const data = {
					token: tokenList
				};
				if (type == 'viot') {
					getApp().globalData.api.getDeviceStatus(data, (result) => {
						console.log(result, '设备状态')
						uni.hideLoading()
						if (result[0].status == 'online') {
							this.deviceLogin(token, type)
						} else {
							uni.showModal({
								title: this.$t('JLink.prompt.title'),
								content: this.$t('connectNet.device_connection_service'),
								showCancel: false
							});
						}
					});
				} else {
					getApp().globalData.api.getRdsDeviceStatus(data, (result) => {
						uni.hideLoading()
						if (result[0].status == 'online') {
							this.deviceLogin(token, type)
						} else {
							uni.showModal({
								title: this.$t('JLink.prompt.title'),
								content: this.$t('connectNet.device_connection_service'),
								showCancel: false
							});
						}
					});
				}
			},
			// 登录设备
			deviceLogin(token, type) {
				uni.showLoading({
					title: this.$t('connectNet.loading')
				})
				let data = {
					Name: 'generalinfo',
					UserName: 'admin',
					PassWord: XMMD5Encrypt(''),
					LoginType: 'DVRIP-Web',
					EncryptType: 'DISABLE',
					KeepaliveTime:20
				};
				if(this.device.adminToken){
					data.LoginToken = this.device.adminToken
				}
				console.log('设备类型',type);
				if (type == 'viot') {
					getApp().globalData.api.deviceLogin(data, token, (result) => {
						uni.hideLoading()
						console.log(result, '登录设备 viot');
						if(result.data.code == 2000 && result.data.data.Ret == 100){
							this.resetPasswords(token)
						}else if(result.data.code !=2000){
							uni.showToast({
								title:result.data.msg,
								icon:'none'
							})
						}else if(result.data.code == 2000 && result.data.data.Ret != 100){
							uni.showToast({
								title:result.data.data.RetMsg || RetCodeMsg[result.data.data.Ret],
								icon:'none'
							})
						}
					});
				} else {
					getApp().globalData.api.deviceRdsLogin(data, token, (result) => {
						uni.hideLoading()
						console.log(result, '登录设备');
						if(result.data.code == 2000 && result.data.data.Ret == 100){
							this.resetPasswords(token)
						}else if(result.data.code !=2000){
							uni.showToast({
								title:result.data.msg,
								icon:'none'
							})
						}else if(result.data.code == 2000 && result.data.data.Ret != 100){
							uni.showToast({
								title:result.data.data.RetMsg,
								icon:'none'
							})
						}
					});
				}
			},
			// 设置重置设备密码
			resetPasswords(token) {
				uni.showLoading({
					title:this.$t('connectNet.loading')
				})
				let data = {
					Name: 'MODIFYPASSWORD',
					UserName: 'admin',
					PassWord: XMMD5Encrypt(''),
					NewPassWord: XMMD5Encrypt(this.confirmPass),
					EncryptType: 'DISABLE'
				};
				getApp().globalData.api.usermanage(data, token, (result) => {
					uni.hideLoading()
					console.log('1===================:',result);
					if (result.Ret === 100) {
						this.device.devPassWord = data.NewPassWord;
						this.deviceLogOut(token)
						uni.reLaunch({
							url: '/jvss/pagesDevice/device/bluetooth/equipment-rename?device=' + encodeURIComponent(JSON.stringify(this.device))
						});
					} else {
						uni.showModal({
							title: this.$t('JLink.prompt.title'),
							content: result.retMsg,
							showCancel: false
						});
					}
				});
			},
			deviceLogOut(token){
				let data = {
					Name: 'Logout',
				};
				getApp().globalData.api.deviceLogOut(data, token, (result) => {
					console.log(result,'登出')
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.set-head {
		text-align: center;
		font-size: 26rpx;

		p:nth-child(1) {
			margin: 40rpx 0 20rpx 0;
			color: #666;
		}

		p:nth-child(2) {
			color: #333;
		}
	}

	.set-input {
		margin: 100rpx 0 20rpx 0;
	}

	/deep/.uni-easyinput {
		width: 96% !important;
		height: 80rpx;
		background: #fff;
		border-radius: 16rpx;
		margin: 20rpx auto;
		// border: #333 1px solid;
	}

	/deep/.uni-easyinput .is-input-border {
		border: none;
		// border: 1rpx solid #DCDFE6;
		height: 100%;
	}

	.set-foot {
		margin: auto;
		width: 80%;
		color: #666;
		font-size: 26rpx;
	}
	.info-img {
		width: 30rpx;
		height: 30rpx;
		margin-right: 10rpx;
	}
	.reg{
		color: #FF0025;
		font-size: 26rpx;
	}
	.pwdDesc{
		margin-top: 30rpx;
		color: #000;
		font-size: 26rpx;
	}
	.red{
		color: red;
	}
</style>